Hi guys,
It's been a while since I did anything with C++, but I am attempting to get back into it for the writing of my own little operating system. As I can't use any external headers or libraries, I have decided to create my own set of libraries based loosely on the .NET framework.
I have started by creating a simple string class using linked lists. But it's not working . It's been a while since I used pointers so that's probably where the problem is.
I'll post the code that I think is causing the problem below, and I'll also upload the VC++ 6 project and source. If anyone could take a look and tell me where I've gone wrong, I would really appreciate it.
The string code is broken into two classes - String and StringNode. String contains a member called "FirstNode", which points to the first StringNode in the list (the first character).
The problem seems to be that FirstNode is not being assigned by my Add(char) function.
Main() - DebugMain.cpp
PHP Code:
#include "System.String.hpp"
#include <iostream>
using namespace std;
using namespace System;
int main()
{
String * s = new String("Hello");
*s = "Yellow";
s->Traverse();
cout << s->ToChar() << endl;
delete s;
return 0;
}
System.String.Hpp - String Header File
PHP Code:
#ifndef SYSTEM_STRING_H
#define SYSTEM_STRING_H 1
namespace System
{
class String
{
public:
String();
String(const char *);
const char operator[](const int Index);
String &operator+=(const char &);
String &operator=(const char * chararray);
bool Add(const char C);
bool Add(String);
bool Clear();
const char * ToChar();
~String();
void Traverse();
private:
class StringNode
{
public:
StringNode();
StringNode(const char wC);
char C;
StringNode * Next;
};
StringNode * FirstNode;
};
}
#endif
Add(char) Function
PHP Code:
//-----------------------------------------------------------------------------
/// <summary>
/// Adds a character to the String.
/// </summary>
/// <param name="C">The character to add.</param>
/// <returns>True if the character was added.</returns>
//-----------------------------------------------------------------------------
bool System::String::Add(const char C)
{
cout << "Add(" << C << ") called." << endl;
StringNode * node = FirstNode;
cout << "&*node: " << (&*node) << endl;
while (node != 0)
{
node = node->Next;
}
node = new StringNode(C);
cout << "&*node: " << (&*node) << endl;
cout << "&*FirstNode: " << (&*FirstNode) << endl;
return true;
}
String Constructor
PHP Code:
System::String::String(const char * Text)
{
this->FirstNode = 0;
int X = 0;
while (Text[X] != 0 && Text[X] != '\0')
{
this->Add(Text[X]);
X++;
}
}
When run, FirstNode has the address of 0 even after Add() has been called. At the start of Add(), doesn't my logic mean a new StringNode will be created and assigned where FirstNode is?
Thank you to anyone who can help!
Paul Stovell